package com.zld.smarket.service.serviceImpl;

import com.zld.smarket.dao.GoodsMapper;
import com.zld.smarket.dto.GoodsList;
import com.zld.smarket.pojo.Goods;
import com.zld.smarket.result.ExceptionMsg;
import com.zld.smarket.result.ResponseData;
import com.zld.smarket.service.GoodsService;
import com.zld.smarket.util.UpUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;
import org.springframework.transaction.interceptor.TransactionAspectSupport;
import org.springframework.web.multipart.MultipartFile;

import javax.servlet.http.HttpServletRequest;
import java.util.List;

@Service
public class GoodsServiceimpl implements GoodsService {
    @Autowired
    private GoodsMapper goodsMapper;
    @Override
    public ResponseData findList(Integer page, Integer limit) {
        if(page != null && limit!=null){
            page = (page-1)*limit;
        }else{
            page = 0;
            limit = 10;
        }
        List<GoodsList> goodsLists = goodsMapper.selectAllAndPage(page,limit);
        Long count = goodsMapper.countGoods();
        return new ResponseData(goodsLists,count);
    }

    @Override
    public Integer addGoods(Goods goods) {
        int j=0;
        j=goodsMapper.insert(goods);
        return j;
    }


    @Transactional
    public Integer deleteById(Long[] ids) {
        int j=0;
        try {
            //先判定数据是否存在
            for (Long id : ids) {
                Goods goods = goodsMapper.selectByPrimaryKey(id);
                if(goods != null) {
                    j = goodsMapper.deleteByPrimaryKey(id);
                }
            }
        }catch(Exception e){
            e.printStackTrace();
//            设置手动回滚
            TransactionAspectSupport.currentTransactionStatus().setRollbackOnly();
        }
        return j;
    }

    @Override
    public ResponseData search(String s,Integer page, Integer limit) {
        if(page != null && limit!=null){
            page = (page-1)*limit;
        }else{
            page = 0;
            limit = 10;
        }

        List<GoodsList> goodsLists =  goodsMapper.search(s,page,limit);
        Long count = Long.valueOf(goodsLists.size());
        return new ResponseData(goodsLists,count);
    }

    @Override
    public ResponseData updateById(Goods goods) {
        try {
            Goods goods1 = goodsMapper.selectByPrimaryKey(goods.getGoodsId());
            //还需要加逻辑判定  -- 去学习正则表达式
            if(goods1!=null){
                int i =  goodsMapper.updateByPrimaryKey(goods);
                if(i==0){
                    return new ResponseData(ExceptionMsg.FAILD);
                }else{
                    return new ResponseData(ExceptionMsg.SUCCESS);
                }
            }else {
                return new ResponseData("000407","请刷新以后在尝试！");
            }

        }catch (Exception e){
            e.printStackTrace();
            TransactionAspectSupport.currentTransactionStatus().setRollbackOnly();
            return new ResponseData(ExceptionMsg.FAILD);
        }

    }

    @Override
    public Long selectbyno(Long goodsNo) {
        return goodsMapper.countGoodsbyno(goodsNo);
    }

    @Override
    public List<Goods> selectAll() {
        return goodsMapper.selectAll();
    }

    @Override
    public ResponseData upimg(MultipartFile file, HttpServletRequest request) {
        Integer upfile = UpUtils.upfile(file, request);
        if(upfile == 1){
            return  new ResponseData(ExceptionMsg.SUCCESS,file.getOriginalFilename());
        }else{
            return new ResponseData(ExceptionMsg.FAILD);
        }
    }

}
